package com.masterclient.app.businessmodule.homemodule.view.adapter.recommand;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.commonbean.product.CommodityOversea;
import com.eoner.commonbean.product.ProductTag;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.shoplist.ProductHomeMessage;
import com.masterclient.app.modules.goods.newpage.activity.NewCommodityDetailActivity;

import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * 作者：sean
 * 创建时间：2017/6/23 16:32
 * 功能说明：
 */

public class HomeGoodsRecyclerAdapter extends BaseQuickAdapter<ProductHomeMessage, BaseViewHolder> {

    private Typeface typeface;
    private Transformation<Bitmap>[] goodsTransformation;
    private Transformation<Bitmap>[] circleTransformation;

    public HomeGoodsRecyclerAdapter() {
        super(R.layout.item_footer_good);
        this.strings = new String[]{};
    }

    private Transformation<Bitmap>[] getGoodsTransformation() {
        if (goodsTransformation == null) {
            goodsTransformation = new Transformation[]{new CenterCrop(),
                    new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0, RoundedCornersTransformation.CornerType.TOP)};
        }
        return goodsTransformation;
    }

    public Transformation<Bitmap>[] getCircleTransformation() {
        if (circleTransformation == null) {
            circleTransformation = new Transformation[]{new CenterCrop()};
        }
        return circleTransformation;
    }

    private Typeface getTypeface() {
        if (typeface == null) {
            typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/iconfont.ttf");
        }
        return typeface;
    }

    private String[] strings;

    public void setStrings(String... strings) {
        this.strings = strings;
    }

    @Override
    protected void convert(BaseViewHolder helper, ProductHomeMessage item) {

        //渲染价格
        renderAllPrice(helper,item);

        TextView tvFavoriteNum =  helper.getView(R.id.tv_favorite_num);
        tvFavoriteNum.setText(item.getFavorite_num()+"人喜欢");

        ImageView imageView =  helper.getView(R.id.iv_goods);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        layoutParams.height = item.getHomeWidth();
        layoutParams.width = item.getHomeWidth();
        ViewTransformUtil.glideImageView(mContext,item.getImage(),imageView,
                R.mipmap.defaults);

        List<ProductTag> tags =  item.getTags();
        String new_member_tag = "";//新人标签
        String present_tag = "";//赠品标签
        String activity_mj_tag = "";//满减标签
        String deduction_rate_tag = "";//金币抵扣
        if (tags != null && tags.size()>0) {
            for (ProductTag tag : tags) {
                if ("2".equals(tag.getType())) {
                    new_member_tag = tag.getName();
                } else if ("3".equals(tag.getType())) {
                    activity_mj_tag = tag.getName();
                } else if ("4".equals(tag.getType())) {
                    present_tag = tag.getName();
                } else if ("5".equals(tag.getType())) {
                    deduction_rate_tag = tag.getName();
                }
            }
        }

        String country = "",country_logo = "",country_tag = "";
        CommodityOversea oversea = item.getOversea();
        if(oversea != null){
            country = oversea.getCountry_name();
            country_logo = oversea.getCountry_logo();
            country_tag = oversea.getTag();
        }

        helper.setText(R.id.tv_sub_title,item.getTitle());

        ImageView ivTagNew = helper.getView(R.id.iv_tag_new);

        if("1".equals(item.isIs_new())){
            ivTagNew.setVisibility(View.VISIBLE);
        }else {
            ivTagNew.setVisibility(View.GONE);
        }

        renderActivityImage(helper,item);

        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NewCommodityDetailActivity.class);
                intent.putExtra("id",item.getProduct_id());
                intent.putExtra("source_page", strings.length>0?strings[0]:"");
                intent.putExtra("source_module",strings.length>1?strings[1]:"");
                intent.putExtra("source_element",strings.length>2?strings[2]:"");
                intent.putExtra("key_word", strings.length>3?strings[3]:"");
                intent.putExtra("key_word_type", strings.length>4?strings[4]:"");
                intent.putExtra("source_position",helper.getAdapterPosition()-getHeaderLayoutCount()+"");
                mContext.startActivity(intent);
            }
        });
    }

    private void renderActivityImage(BaseViewHolder helper, ProductHomeMessage item) {

        ImageView ivActivity = helper.getView(R.id.iv_activity);
        if(item.getIcon_info()!=null
                && !TextUtils.isEmpty(item.getIcon_info().iconHeight)
                &&!TextUtils.isEmpty(item.getIcon_info().iconWidth)) {

            int leftMargin = (int) (Integer.parseInt(item.getIcon_info().iconLeftMargin)*item.getRatio());
            int topMargin = (int) (Integer.parseInt(item.getIcon_info().iconTopMargin)*item.getRatio());
            int bottomMargin = (int) (Integer.parseInt(item.getIcon_info().iconBottomMargin)*item.getRatio());
            ViewTransformUtil.glideImageView(mContext,item.getIcon_info().iconImage,ivActivity,R.mipmap.defaults);
            ivActivity.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivActivity.getLayoutParams();
            params.height = (int) (Integer.valueOf(item.getIcon_info().iconHeight)*item.getRatio());
            params.width = (int) (Integer.valueOf(item.getIcon_info().iconWidth)*item.getRatio());
            params.addRule(RelativeLayout.ALIGN_BOTTOM,0);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);
            params.setMargins(0, 0, 0, 0);
            switch (item.getIcon_info().iconMarginType) {
                case "1":
                    params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.iv_goods);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
                case "2":
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
            }
            ivActivity.setLayoutParams(params);
        }else {
            ivActivity.setVisibility(View.GONE);
        }
    }

    private void renderAllPrice(BaseViewHolder helper, ProductHomeMessage item) {
        TextView tvPrice = helper.getView(R.id.price);
        TextView tvOfficialPrice = helper.getView(R.id.tv_official_price);

        Utils.set_price_style2(tvPrice,"¥ " + item.getPrice());
        Utils.setDin(tvPrice,mContext);
        tvOfficialPrice.setText("¥" +item.getLine_price());
        tvOfficialPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        tvOfficialPrice.setVisibility(View.VISIBLE);
    }
}
